import { NumberPanel } from './numberpanel.component';
import { MdDialog, MdDialogConfig, MdDialogRef } from '@angular/material';
import { Injectable } from '@angular/core';

@Injectable()
export class NumberPanelService {
    public static current: NumberPanelService = new NumberPanelService();
    dialogRef: MdDialogRef<NumberPanel>;
    private tempNumPanel: NumberPanel;
    dialogConfigs: MdDialogConfig = {
        disableClose: false,
        width: '',
        height: '',
        position: {
            top: '',
            bottom: '0',
            left: '0',
            right: '0'
        }
    }
    /**
     * 打开数字输入面板
     * @param dialog material的MdDialog对象
     * @param config material的dialog的配置对象
     */
    open(dialog: MdDialog, title: string, maxNumber?: number, inputNum?: string, isDecimal?: boolean, config?: MdDialogConfig) {
        let configSelf = config ? config : this.dialogConfigs;
        this.dialogRef = dialog.open(NumberPanel, configSelf);
        if (title) {
            this.tempNumPanel.title = title;
        }
        if (maxNumber) {
            this.tempNumPanel.maxNumber = maxNumber;
        }
        if (inputNum) {
            this.tempNumPanel.inputNum = inputNum + "";
        }
        this.tempNumPanel.isDecimal = isDecimal;
        return new Promise((resolve, reject) => {
            this.dialogRef.afterClosed().subscribe(data => {
                resolve(data);
            });
        });
    }

    regist(numberPanel: NumberPanel) {
        this.tempNumPanel = numberPanel;
    }
}